<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Imgcate extends Model
{
    protected $table = 'imgcate';
    public $timestamps = false;
    protected $guarded = [
      ];


    public function getData($search = [], $page = 1, $length = 20){
        $ImgcateList = array();
        $ImgcateList['pageData'] = array();
        $date = date('Y-m-d H:i:s', time());

        $sql = \DB::table($this->table)
            ->whereNull('deleted_at');

        if(isset($search['type'])){
            $sql->where('type',$search['type']);
        }

        $ImgcateList['pageData']['count']     = $sql->count();
        $ImgcateList['pageData']['lastPage']  = ceil($ImgcateList['pageData']['count'] / $length);

        $ImgcateList['list'] = $sql->skip(($page-1) * $length)->take($length)
            ->get();

        return $ImgcateList;
    }
    //添加
    public function saveData($data){
        return \DB::table($this->table)->insert($data);
    }

    //删除
    public function Del($data,$id){
        return \DB::table($this->table)->where('id',$id)->update($data);
    }


    //图片列表
    public function getimglist($search = [], $page = 1, $length = 20,$id){
        $ImgList = array();
        $ImgList['pageData'] = array();
        $date = date('Y-m-d H:i:s', time());

        $ImgcateImgModel = new ImgcateImg();
        $sql = \DB::table($ImgcateImgModel->getTable() . ' as img')
            ->leftjoin($this->table . ' as cate','cate.id','img.type_id')
            ->select('img.id','cate.type','img.title','img.img','img.type_id','img.updated_at')
            ->where('img.type_id',$id)
            ->whereNull('img.deleted_at');

        if(isset($search['title'])){
            $sql->where('img.title','like' ,"%".$search['title']."%");
        }

        $ImgList['pageData']['count']     = $sql->count();
        $ImgList['pageData']['lastPage']  = ceil($ImgList['pageData']['count'] / $length);

        $ImgList['list'] = $sql->skip(($page-1) * $length)->take($length)
            ->get();

        return $ImgList;
    }

    //图片添加
    public function imgadd($data){
        $ImgcateImgModel = new ImgcateImg();
        return \DB::table($ImgcateImgModel->getTable())->insert($data);
    }
    //图片删除
    public function imgdel($data,$id){
        $ImgcateImgModel = new ImgcateImg();
        return \DB::table($ImgcateImgModel->getTable())->where('id',$id)->update($data);
    }
}